﻿#include "UMainInfoPanel.h"
#include "CRobTemplateMngDlg.h"
#include "CRobTaskMngDialog.h"
#include "CTaskExecQueryDlg.h"
UMainInfoPanel::UMainInfoPanel( QWidget *parent)
{
	ui.setupUi(this);
}


UMainInfoPanel::~UMainInfoPanel()
{	
	if (m_pTimer != nullptr)
	{
		m_pTimer->stop();
		delete m_pTimer;
		m_pTimer = nullptr;
	}
}
void UMainInfoPanel::init()
{
	ui.widgetBatteryShow->setBorderColor(QColor(0, 0, 0));
	ui.widgetBatteryShow->setPaddingColor(QColor(0, 255, 0));
	//配置定时器
	m_pTimer = new QTimer(this);
	m_pTimer->setTimerType(Qt::TimerType::PreciseTimer);
	connect(m_pTimer, &QTimer::timeout, this, &UMainInfoPanel::slotDataRefresh);
	connect(ui.pushButton_Template, &QPushButton ::clicked, this, &UMainInfoPanel::slotTemplate);
	connect(ui.pushButton_Task, &QPushButton::clicked, this, &UMainInfoPanel::slotTaskMng);
	connect(ui.pushButton_TaskResult, &QPushButton::clicked, this, &UMainInfoPanel::slotTaskInfo);
}
void UMainInfoPanel::setStyleSheetFile()
{
	//QString sQssPath = ":/qss/styleSheet.css";
	//QFile qss(sQssPath);
	//qss.open(QFile::ReadOnly);
	//this->setStyleSheet(qss.readAll());
}

void UMainInfoPanel::setRobSpeed(double dSetSpeed, double dCurrSpeed)
{
	//ui.labelSpeedInfoSet->setText(QString::number(dSetSpeed, 'f', 2));
	ui.labelSpeedInfoCurrent->setText(QString::number(dCurrSpeed, 'f', 2));
}

void UMainInfoPanel::setWorkNum(int nlInspectCount, int nCheckedCount, int nAlarmCount)
{
	ui.labelInspectionCount->setText(QString::number(nlInspectCount));
	ui.labelCheckedCount->setText(QString::number(nCheckedCount));
	ui.labelAlarmCount->setText(QString::number(nAlarmCount));
}

void UMainInfoPanel::setBattery(int nBattery, bool bIsCharging/*=false*/)
{
	ui.widgetBatteryShow->setLevel(nBattery);
	ui.labelBatteryValue->setText(QString::number(nBattery) + "%");
	if (bIsCharging == true)
	{
		ui.widgetBatteryShow->setStatus(CBasicBattery::BatteryStatus::Charging);
	}
	else
	{
		ui.widgetBatteryShow->setStatus(CBasicBattery::BatteryStatus::Discharging);
	}
}

void UMainInfoPanel::setTaskProcess(int nProcess)
{
	ui.progressBarTask->setValue(nProcess);
}
void UMainInfoPanel::slotDataRefresh()
{
	if (!isVisible())
	{
		return;
	}
	double dSetSpeed = 0.0;
	CDevDataBase* pDevData = (CDevDataBase*)m_pDev->m_pDevDataBase;
	if (pDevData->m_nRobRunMode == 0)//遥控模式
	{
		setTaskProcess(0);
	}
	else if (pDevData->m_nRobRunMode == 1)//任务模式
	{
		if (pDevData->m_nTaskStatus == 4)
		{
			setTaskProcess(0);
		}
		else
		{
			//获取任务信息
			if (m_pBasicData->m_pMutex->tryLock(200))
			{
				setWorkNum(m_pBasicData->m_pTaskExecInfo->m_nItemCount, m_pBasicData->m_pTaskExecInfo->m_nExecItemCount, m_pBasicData->m_pTaskExecInfo->m_nAlarmItemCount);
				if (m_pBasicData->m_pTaskExecInfo->m_nItemCount != 0)
				{
					setTaskProcess(((double)m_pBasicData->m_pTaskExecInfo->m_nExecItemCount / (double)m_pBasicData->m_pTaskExecInfo->m_nItemCount) * 100);
				}
				else
				{
					setTaskProcess(0);
				}
				m_pBasicData->m_pMutex->unlock();
			}
		}
	}
	pDevData->m_pMutex->lock();
	dSetSpeed = pDevData->m_sRobCurrSpeed.toDouble();
	setRobSpeed(dSetSpeed, pDevData->m_sRobCurrSpeed.toDouble());

	bool bCharging = false;//是否在充电
	if (pDevData->m_sDevWorkStatus.toInt() == 3)
	{
		bCharging = true;
	}
	else
	{
		bCharging = false;
	}
	setBattery(pDevData->m_sRobBatteryInfo.toDouble(), bCharging);
	pDevData->m_pMutex->unlock();
}


void UMainInfoPanel::slotTemplate()
{
	CRobTemplateMngDlg oper(m_sGuid, m_pBasicData);
	oper.exec();
}
void UMainInfoPanel::slotTaskMng()
{
	CRobTaskMngDialog oper(m_sGuid, m_pBasicData);
	oper.exec();
}
void UMainInfoPanel::slotTaskInfo()
{
	CTaskExecQueryDlg oper(m_sGuid, m_pBasicData);
	oper.exec();
}